<?php
namespace Admin\Controller;
use Admin\Controller\AdminController;
use Think\Page;
class WithdrawController extends AdminController {
 	//xuxiang
	public function _initialize(){
 		parent::_initialize();
	}
	/**
	 * 获取提现列表
	 */
	public function withdrawList(){
		//获取提现列表
		//审核
		//否，退换积分
		//是，线下交易
		$address = I('address');
		if(!empty($address)){
			$where['address'] = $address;
		}
		$post = array('address'=>$address);
		$count = M("Withdraw")->where($where)->order('add_time desc')->count();
		$Page  = new \Think\Page($count,20);
		setPageParameter($Page, $post);
		$show = $Page->show();
		$list = M('Withdraw')->where($where)->order("add_time desc")->limit($Page->firstRow.','.$Page->listRows)->select();
		foreach($list as $k=>$v){
			$nickname = M('Member')->field('nickname')->where(array('openid'=>$v['open_id']))->find()['nickname'];
			$list[$k]['nickname'] = $nickname;
		}
		$this->assign('list',$list);
		$this->assign('page',$show);
		$this->display();
	}
	
	/**
	 * 修改审核状态
	 */
	public function audit(){
		$withdraw_id = I('withdraw_id');
		$status = I('status');
		$info = M('Withdraw')->where(array('withdraw_id'=>$withdraw_id))->find();
		M()->startTrans();
		$re[] = M('Withdraw')->where(array('withdraw_id'=>$withdraw_id))->setField('status',$status);
		$re[] = M('Withdraw')->where(array('withdraw_id'=>$withdraw_id))->setField('audit_time',time());
		if($status == 2){
			$re[] = M('Member')->where(array('openid'=>$info['open_id']))->setInc('money',intval($info['withdraw_money']));
			$re[] = $this->addFinance($info['open_id'],1,1,$info['withdraw_money'],"提现驳回");
		}
		if(in_array(false, $re)){
			M()->rollback();
			$this->error('操作失败，请重新尝试');
			exit();
		}else{
			M()->commit();
			if($status == 2){
			    
				$this->success('操作成功，提现金额已返回会员',U('Withdraw/withdrawList'));
				exit();
			}else{
				$this->success('操作成功，请尽快转款',U('Withdraw/withdrawList'));
				exit();
			}
		}
	}
}